/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.common.dfs;

import com.koron.common.Tools;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@Component
public class FileServer {
	
	private final static  Logger logger = Logger.getLogger(FileServer.class);
	
	@Value("${breakpoint.upload.dir}")
    private String defaultDir;

//	@Value("${file.preview}")
//	private String preview;
	
	public String uploadFile(MultipartFile multipartFile) throws IOException {
		//获取文件原始名
        String originalFilename = multipartFile.getOriginalFilename();
        //获取文件后缀名 不含点
        String extension = FilenameUtils.getExtension(originalFilename);
        String fileName = Tools.getCode32() + "." + extension;
		File dir = new File(defaultDir);
		if (dir.exists()) {
			if (dir.isFile()) {
				dir.delete();
				dir.mkdirs();
			}
		} else {
			dir.mkdirs();
		}
		File file = new File(defaultDir + fileName);
		logger.debug("文件上传目录："+defaultDir+fileName);
        //如果这个文件不存在
        if (!file.exists()) {
        	//创建一个
            file.createNewFile();
        }
        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(),file);
        //文件上传
        multipartFile.transferTo(file);
        return fileName;
        
	}
	
	public String uploadFile(InputStream inputStream, long fileSize, String fileExtName) throws IOException {
		String fileName = Tools.getCode32() + "." + fileExtName;
		File dir = new File(defaultDir);
		if (dir.exists()) {
			if (dir.isFile()) {
				dir.delete();
				dir.mkdirs();
			}
		} else {
			dir.mkdirs();
		}
		File file = new File(defaultDir + fileName);
		MultipartFile multipartFile = new MockMultipartFile(fileName,inputStream);
		multipartFile.transferTo(file);
		return fileName;
	}
	
	public void deleteFile(String fileUrl) {
		if (StringUtils.isEmpty(fileUrl)) {
            return;
        }
		String temp = fileUrl.substring(fileUrl.lastIndexOf("/")+1);
		File file = new File(defaultDir + temp);
		if(file != null) {
			file.delete();
		}
	}

	/**
	 * 下载文件
	 * @param fileUrl
	 * @return
	 */
	public InputStream downloadFile(String fileUrl) {
		if (StringUtils.isNotEmpty(fileUrl)) {
			String temp = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
			temp = temp.split("#")[0];
			try {
				return new FileInputStream(defaultDir + temp);
			} catch (FileNotFoundException e) {
				logger.error(e.getMessage(),e);
			}
		}
		return null;
	}
}
